//
//  DMetabolismViewController.m
//  dance
//
//  Created by aether on 2023/10/20.
//

#import "DMetabolismViewController.h"
#import "DInfoViewController.h"

@interface DMetabolismViewController () <UITextFieldDelegate>

@property (nonatomic, strong) IBOutlet UIView *bg;
@property (nonatomic, weak) IBOutlet UIView *loginBg;
@property (nonatomic, weak) IBOutlet UIButton *sexBtn;
@property (nonatomic, weak) IBOutlet UITextField *age;
@property (nonatomic, weak) IBOutlet UITextField *weight;
@property (nonatomic, weak) IBOutlet UITextField *height;
@property (nonatomic, weak) IBOutlet UILabel *remind;
@property (nonatomic, weak) IBOutlet UITextView *formula;
@property (nonatomic, assign) bool isMan;

@end

@implementation DMetabolismViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initValues];
}
- (void) initValues {
    self.isMan = true;
}

- (void) initViews {
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame) + 20, CGRectGetHeight(self.view.frame));
    gl.startPoint = CGPointMake(0.5, 0);
    gl.endPoint = CGPointMake(0.5, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:71/255.0 green:0/255.0 blue:233/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:51/255.0 green:149/255.0 blue:247/255.0 alpha:0.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    [self.bg.layer addSublayer:gl];
        
    CAGradientLayer *gl1 = [CAGradientLayer layer];
    gl1.frame = CGRectMake(0, 0, CGRectGetWidth(self.loginBg.frame) + 20, CGRectGetHeight(self.loginBg.frame));
    gl1.startPoint = CGPointMake(0, 0.5);
    gl1.endPoint = CGPointMake(1, 0.5);
    gl1.colors = @[(__bridge id)[UIColor colorWithRed:1/255.0 green:140/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:68/255.0 green:57/255.0 blue:229/255.0 alpha:1.0].CGColor];
    gl1.locations = @[@(0), @(1.0f)];
    self.loginBg.layer.cornerRadius = 7;
    self.loginBg.layer.masksToBounds = YES;
    [self.loginBg.layer addSublayer:gl1];
    
    
    if (self.type == 0)
        [self.formula setText:@"计算公式：对于男性：BMR = 88.362 + (13.397 × 体重，单位为千克) + (4.799 × 身高，单位为厘米) - (5.677 × 年龄，单位为岁) \n对于女性：BMR = 447.593 + (9.247 × 体重，单位为千克) + (3.098 × 身高，单位为厘米) - (4.330 × 年龄，单位为岁) \n 此数据仅供参考，在做出医疗决定之前应该咨询医生"];
    else if (self.type == 1)
        [self.formula setText:@"计算公式：1.2+体重指数+0.23×年龄-5.4-10.8×性别（女是0，男是1）。 \n 此数据仅供参考，在做出医疗决定之前应该咨询医生"];
    else if (self.type == 3)
        [self.formula setText:@"计算公式：血容量 = 70 mL/kg × 体重（千克） \n 此数据仅供参考，在做出医疗决定之前应该咨询医生"];
    
    
//    // 创建右侧的UIBarButtonItem
//    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:<#(nullable NSString *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>:UIBarButtonSystemItemSearch target:self action:@selector(rightButtonTapped:)];
//    
//    // 设置右侧按钮
//    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"来源" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonTapped:)];

    // 添加UIBarButtonItem到导航栏
    self.navigationItem.rightBarButtonItem = customButtonItem;

}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self initViews];
}

#pragma mark - 事件


- (void) rightButtonTapped:(UIBarButtonItem *) item {
    
//    DInfoViewController *dinfo = [[DInfoViewController alloc] initWithNibName:@"DInfoViewController" bundle:nil];
//    
//    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:@"代谢", @"title", @"https://m.baidu.com/bh/m/detail/vc_9400833542949424160", @"html", nil];
//    
//    [dinfo setInfo:info];
//    [self.navigationController pushViewController:dinfo animated:YES];
    
    NSString *urlString = @"https://m.baidu.com/bh/m/detail/vc_9400833542949424160";
    if (self.type == 0)
        urlString = @"https://m.baidu.com/bh/m/detail/vc_9400833542949424160";
    else if (self.type == 1)
        urlString = @"https://m.baidu.com/bh/m/detail/ar_10760581708316768090";
    else if (self.type == 3)
        urlString = @"https://m.baidu.com/bh/m/detail/vc_12074442211018268379";

    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];

}

- (IBAction) sexClicked:(id)sender {
    self.isMan = !self.isMan;
    [self.sexBtn setTitle:self.isMan ? @"男性" : @"女性" forState:UIControlStateNormal];
}

- (IBAction) computeClicked:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"该功能开发中，敬请期待" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"提示" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 处理确定按钮的操作
    }];

    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    return;
    
    
    if (self.type == 0)
        [self calculateBMR];
    else if (self.type == 1)
        [self calculateFat];
    else if (self.type == 3)
        [self calculateBlood];
}

#pragma mark - 代理方法

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    return true;;
}

#pragma mark - 内部方法

- (void) calculateBMR {
    int age = [self.age.text intValue];
    int weight = [self.weight.text intValue];
    int height = [self.height.text intValue];
    double result = 0;
    
    if (self.isMan)
        result = [self calculateBMRForMaleWithWeight:weight height:height age:age];
    else
        result = [self calculateBMRForFemaleWithWeight:weight height:height age:age];
    
    self.remind.text = [NSString stringWithFormat:@"你的基础代谢率是：%f 卡路里/天", result];
}

- (void) calculateFat {
    int age = [self.age.text intValue];
    int weight = [self.weight.text intValue];
    int height = [self.height.text intValue];
    
    double factor = self.isMan ? 0.292 : 0.296;
    
    // 计算BMI
    double bmi = (weight / (height * height)) * 10000; // 转换身高为米
    
    // 使用BMI和年龄估算脂肪百分比
    double bodyFatPercentage = (factor * bmi) + (0.158 * age) - 5.41;
    
    self.remind.text = [NSString stringWithFormat:@"你的体脂是：%f", bodyFatPercentage];
}

- (void)calculateBlood {
    int weight = [self.weight.text intValue];
    double bloodVolume = 70.0 * weight;
    self.remind.text = [NSString stringWithFormat:@"你的血容量是：%f 升", bloodVolume];
}


// 计算男性BMR
- (double)calculateBMRForMaleWithWeight:(double)weight height:(double)height age:(int)age {
    double bmr = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age);
    return bmr;
}

// 计算女性BMR
- (double)calculateBMRForFemaleWithWeight:(double)weight height:(double)height age:(int)age {
    double bmr = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);
    return bmr;
}

@end
